﻿Imports System.Data.OleDb

Public Class ApprovePromotion
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub grvPromotions_SelectedIndexChanged(sender As Object, e As EventArgs) Handles grvPromotions.SelectedIndexChanged

    End Sub

    Protected Sub btnApprove_Click(sender As Object, e As EventArgs) Handles btnApprove.Click
        Try


            If (grvPromotions.SelectedValue <> Nothing) Then
                'upgrade user
                Dim getPromotion As PromotionsRequestManagement = New PromotionsRequestManagement()

                Dim dr As OleDbDataReader = getPromotion.SelectPromotionByPromotionId(Convert.ToInt32(grvPromotions.SelectedValue))
                Dim userIdToChange As Int32 = -1
                Dim promotionTypeId As Int32 = -1

                If dr.HasRows Then
                    dr.Read()
                    If Not IsDBNull(dr("UserId")) Then userIdToChange = dr("UserId")
                    If Not IsDBNull(dr("PromotionTypeId")) Then promotionTypeId = dr("PromotionTypeId")
                End If

                Dim userRole As Int32
                If (promotionTypeId = 1) Then
                    userRole = 1
                ElseIf (promotionTypeId = 2) Then
                    userRole = 3
                End If
                'check if user already has that role
                Dim checkExistance As aspNet_UsersInRolesManagement = New aspNet_UsersInRolesManagement()
                Dim exists As Int32 = checkExistance.SelectCheckUserRoleExistance(userIdToChange, userRole)

                If (exists = 0) Then
                    'insert new role
                    Dim insertRole As aspNet_UsersInRolesManagement = New aspNet_UsersInRolesManagement()
                    insertRole.InsertUserInRole(userIdToChange, userRole)
                Else
                    lblMessage.Text = "User was already ranked that level. Update not done. Proposal Deleted"
                End If
                'delete proposition
                Dim delete As PromotionsRequestManagement = New PromotionsRequestManagement()
                delete.DeleteRequestByRequestID(Convert.ToInt32(grvPromotions.SelectedValue))

                grvPromotions.DataBind()

            Else
                lblMessage.Text = "Select Value First"
            End If

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnDeny_Click(sender As Object, e As EventArgs) Handles btnDeny.Click
        Try

        
        If (grvPromotions.SelectedValue <> Nothing) Then
            'delete proposition
            Dim delete As PromotionsRequestManagement = New PromotionsRequestManagement()
            delete.DeleteRequestByRequestID(Convert.ToInt32(grvPromotions.SelectedValue))
        Else
            lblMessage.Text = "Select Value First"
        End If

            grvPromotions.DataBind()
        Catch ex As Exception

        End Try
    End Sub
End Class